package cn.kgc.controller;

import cn.kgc.grace.result.GraceJSONResult;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author YC
 */
@RestController
@RequestMapping("/cache")
public class CaffeineController {

    @Resource
    private Cache<String, Object> caffeineCache;

    @GetMapping("/init")
    public GraceJSONResult initCache() {
        caffeineCache.put("resumeCount", 100);
        return GraceJSONResult.ok();
    }

    @GetMapping("/getIfPresent")
    public GraceJSONResult getIfPresent() {
        caffeineCache.put("resumeCount", 100);
        return GraceJSONResult.ok(caffeineCache.getIfPresent("resumeCount"));
    }

    @GetMapping("/get")
    public GraceJSONResult get() {
        Object result = caffeineCache.get("resumeCount", key -> 100);
        return GraceJSONResult.ok(result);
    }

}
